import requests
pins = ["test light","Heat","Air pump","Blue light","Stirrer","Fluid pump"]
def link_status():
    try:
        res = requests.get("http://192.168.4.1",timeout=1.5)
        print("Link Start!")
        return 1
    except:
        print("Failed to connect to the machine...")
        return 0

def reverse_pin(pin_number):
    para = {'pin_choice':pin_number}
    try:
        res = requests.get("http://192.168.4.1/reverse_pin", params=para)
        res.raise_for_status()
        print(pins[pin_number] + ": " + res.text)
        return res.text
    except:
        print("Error: failed to set this function")
        return "Error"

def set_temperature(target_temp):
    para = {'target':target_temp}
    res = requests.get("http://192.168.4.1/set_temperature", params = para)
    if res.status_code == requests.codes.ok:
        print("Temperature set successfully!")
        return res.text
    else:
        print("Error: failed to set the target temperature")
        return "Error"

def get_temperature():
    try:
        res = requests.get("http://192.168.4.1/get_temperature")
        res.raise_for_status()
        print("Current temperature: "+str(res.text))
        return res.text
    except:
        print("Error: failed to get the current temperature")
        return "Error"

def get_set_temperature():
    try:
        res = requests.get("http://192.168.4.1/get_set_temperature")
        res.raise_for_status()
        print("Preset temperature: "+str(res.text))
        return res.text
    except:
        print("Error: failed to get the preset temperature")
        return "Error"

if __name__ == "__main__":
    hint_0 = """Press these keys for certain functions:
0: test if you have connected to the wifi module of the MRM device
1: switch ON/OFF a certain function
2: set a target temperature for the MRM device
3: get the current temperature of the MRM device
4: get the preset target temperature of the MRM device
Type a certain number here: """
    hint_1 = """Decide the function you would like to switch ON/OFF:
0: test light
1: Heat
2: Air pump
3: Blue light
4: Stirrer
5: Fluid pump
Type a certain number here: """
    n = int(input(hint_0))
    if n == 0:
        link_status()
    elif n == 1:
        pin = int(input(hint_1))
        status = reverse_pin(pin)
    elif n == 2:
        temp = int(input("Type in an integer from 0 to 80 to set a temperature here: "))
        temp = set_temperature(temp)
    elif n == 3:
        get_temperature()
    elif n == 4:
        get_set_temperature()